// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: cursor_session.cpp
// Description: CursorSession Class
//      Author: Ziming Li
//     Created: 2022-05-03
// -------------------------------------------------
// Copyright (C) 2022 MingYang Software Technology.
// -------------------------------------------------

#include "./cursor_session.h"

namespace BlinKit {

CursorSession::CursorSession(void) : m_hCursor(LoadCursor(nullptr, IDC_ARROW))
{
}

CursorSession::~CursorSession(void)
{
    if (WebCursorInfo::TypeCustom == m_cursorType)
        DestroyCursor(m_hCursor);
}

bool CursorSession::ProcessSetCursorMessage(WPARAM wParam, LPARAM lParam, LRESULT &result)
{
    const UINT hitTestCode = LOWORD(lParam);
    if (HTCLIENT != hitTestCode)
        return false;

    ::SetCursor(m_hCursor);
    result = TRUE;
    return true;
}

void CursorSession::Update(const WebCursorInfo &cursorInfo)
{
    PCTSTR cursorName = nullptr;

    switch (cursorInfo.type)
    {
        case WebCursorInfo::TypeCross:
            cursorName = IDC_CROSS;
            break;
        case WebCursorInfo::TypeHand:
            cursorName = IDC_HAND;
            break;
        case WebCursorInfo::TypeIBeam:
            cursorName = IDC_IBEAM;
            break;
        case WebCursorInfo::TypeWait:
            cursorName = IDC_WAIT;
            break;
        case WebCursorInfo::TypeHelp:
            cursorName = IDC_HELP;
            break;
        case WebCursorInfo::TypeEastResize:
        case WebCursorInfo::TypeWestResize:
        case WebCursorInfo::TypeEastWestResize:
        case WebCursorInfo::TypeColumnResize:
            cursorName = IDC_SIZEWE;
            break;
        case WebCursorInfo::TypeNorthResize:
        case WebCursorInfo::TypeSouthResize:
        case WebCursorInfo::TypeNorthSouthResize:
        case WebCursorInfo::TypeRowResize:
            cursorName = IDC_SIZENS;
            break;
        case WebCursorInfo::TypeNorthEastResize:
        case WebCursorInfo::TypeSouthWestResize:
            cursorName = IDC_SIZENESW;
            break;
        case WebCursorInfo::TypeNorthWestResize:
        case WebCursorInfo::TypeSouthEastResize:
            cursorName = IDC_SIZENWSE;
            break;
        case WebCursorInfo::TypeNorthEastSouthWestResize:
        case WebCursorInfo::TypeNorthWestSouthEastResize:
        case WebCursorInfo::TypeMove:
            cursorName = IDC_SIZEALL;
            break;
        case WebCursorInfo::TypeProgress:
            cursorName = IDC_APPSTARTING;
            break;
        case WebCursorInfo::TypeNoDrop:
        case WebCursorInfo::TypeNotAllowed:
            cursorName = IDC_NO;
            break;
        case WebCursorInfo::TypeCustom:
            ASSERT(nullptr != cursorInfo.externalHandle);
            break;
        default:
            ASSERT(WebCursorInfo::TypePointer == cursorInfo.type);
            cursorName = IDC_ARROW;
    }

    HCURSOR hNewCursor =
        WebCursorInfo::TypeCustom != cursorInfo.type
        ? LoadCursor(nullptr, cursorName)
        : cursorInfo.externalHandle;
    ::SetCursor(hNewCursor);

    if (WebCursorInfo::TypeCustom == m_cursorType)
        DestroyCursor(m_hCursor);

    m_cursorType = cursorInfo.type;
    m_hCursor    = hNewCursor;
}

} // namespace BlinKit
